#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string>
#include <sys/wait.h>   


class Channel
{
public:
    Channel()
    {

    }
    ~Channel()
    {

    }
private:

};


void printf()
{
    std::cout<<"I am child\n"<<std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Name:" << argv[0] << " options num: 2" << std::endl;
        return 1;//参数不正确
    }

    int num = std::stoi(argv[1]);
    for (int i = 0; i < num; i++)
    {
        int pipefd[2];
        int p = pipe(pipefd);
        if (p < 0)
        {
            std::cout << "pipe error" << std::endl;
            return 2; // 管道创建失败
        }

        pid_t id = fork();
        if(id<0)
        {
            std::cout<<"fork error"<<std::endl;
            return 3;//进程创建失败
        }
        else if(id = 0)
        {
            //子进程
            close(pipefd[1]);
            printf();
            exit(0);
        }

        close(pipefd[0]);
    }

    // std::cout << "helo world\n"<< std::endl;
    return 0;
}